package com.gzy.wbapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.gzy.wbapplication.events.LogoutEvent;
import com.gzy.wbapplication.manager.AuthManager;
import com.gzy.wbapplication.modules.find.FindFragment;
import com.gzy.wbapplication.modules.home.HomeFragment;
import com.gzy.wbapplication.modules.log.LogFragment;
import com.gzy.wbapplication.modules.mine.MineFragment;
import com.gzy.wbapplication.utils.SPUtils;
import com.gzy.wbapplication.views.TabBarItem;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

    int[] ids = {R.id.tabbar_home, R.id.tabbar_find, R.id.tabbar_log, R.id.tabbar_mine};
    TabBarItem[] tabBarItems = new TabBarItem[ids.length];
    Fragment[] fragments = {
            new HomeFragment(),
            new FindFragment(),
            new LogFragment(),
            new MineFragment()
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        for (int i = 0; i < ids.length; i++) {
            tabBarItems[i] = findViewById(ids[i]);
            int finalI = i;
            tabBarItems[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    clickTabbarItem(finalI);
                }
            });
        }


        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        for (Fragment fragment : fragments) {
            fragmentTransaction.add(R.id.fl_content,fragment);
        }
        fragmentTransaction.commit();

        clickTabbarItem(0);

        EventBus.getDefault().register(this);
    }

    public void clickTabbarItem(int index) {
        tabBarItems[index].setSelected(true);
        for (int i = 0; i < tabBarItems.length; i++) {
            if (i != index) {
                tabBarItems[i].setSelected(false);
            }
        }


        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        fragmentTransaction.show(fragments[index]);

        for (int i = 0; i < fragments.length; i++) {
            if (i != index){
                fragmentTransaction.hide(fragments[i]);
            }
        }

        fragmentTransaction.commit();
    }

    public void logout(View view) {
        AuthManager.logout(getApplicationContext());
        startActivity(new Intent(getApplicationContext(), StartActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(LogoutEvent message) {
        finish();
    }
}
